<?php
/**
 * Create account controller
 * @author Kota Kakaji
 *
 */
class ControllerAccountCreate extends Controller {
	//Array of error in form
	private $error = array ();
	
	private $passreset = array ();
	public function index() {
		//Check if user is already logined
		if ($this->customer->isLogged ()) {
			$this->redirect ( $this->url->https ( 'account/account' ) );
		}
		
		//Load language 
		$this->language->load ( 'account/create' );
		$this->language->load ( 'account/forgotten' );
		
		$this->document->title = $this->language->get ( 'heading_title' );
		
		$this->load->model ( 'account/customer' );
		///Validate before process
		if ($this->validateMinh () && $this->validate ()) {
			
			
			$this->passreset ['passrequest'] = $this->request->post ['password'];
			$this->passreset ['activecode'] = md5 ( $this->request->post ['email'] );
			$this->passreset ['username'] = $this->request->post ['username'];
			
			$subject = sprintf ( $this->language->get ( 'mail_subject' ), $this->config->get ( 'config_store' ) );
			$message = sprintf ( $this->language->get ( 'mail_header' ), $this->config->get ( 'config_store' ) ) . "\n\n";
			$message .= sprintf ( 'http://99xu.vn/index.php?route=account/activeaccount&user=' . $this->passreset ['username'] . '&codeActive=' . $this->passreset ['activecode'] . '&active=true' . '&newpass=' . $this->passreset ['passrequest'], $this->config->get ( 'config_store' ) ) . "\n\n";
			$message .= $this->language->get ( 'mail_username' );
			$message .= $this->passreset ['username'] . "\n\n";
			
			
			$message .= $this->language->get ( 'mail_password' );
			
			$message .= $this->passreset ['passrequest'] . "\n\n";
			$message .= sprintf ( $this->language->get ( 'mail_footer' ), $this->config->get ( 'config_store' ) ) . "\n\n";
			$mail = new Mail ( $this->config->get ( 'config_mail_protocol' ), $this->config->get ( 'config_smtp_host' ), $this->config->get ( 'config_smtp_username' ), html_entity_decode ( $this->config->get ( 'config_smtp_password' ) ), $this->config->get ( 'config_smtp_port' ), $this->config->get ( 'config_smtp_timeout' ) );
			$mail->setTo ( $this->request->post ['email'] );
			$mail->setFrom ( $this->config->get ( 'config_email' ) );
			
			$mail->setSender ( $this->config->get ( 'config_store' ) );
			$mail->setSubject ( $subject );
			$mail->setText ( $message );
			$mail->send ();
			
			
			$this->redirect ( $this->url->https ( 'account/success' ) );
			
			
		} 

		else if (($this->request->server ['REQUEST_METHOD'] == 'POST') && $this->validate ()) {
			$this->model_account_customer->addCustomer ( $this->request->post );
			$this->customer->login ( $this->request->post ['username'], $this->request->post ['password'] );
			
			$subject = sprintf ( $this->language->get ( 'mail_subject' ), $this->config->get ( 'config_store' ) );
			$message = sprintf ( $this->language->get ( 'mail_welcome' ), $this->config->get ( 'config_store' ) ) . "\n\n";
			if (! $this->config->get ( 'config_customer_approval' )) {
				$message .= $this->language->get ( 'mail_login' ) . "\n";
			} else {
				$message .= $this->language->get ( 'mail_approval' ) . "\n";
			}
			$message .= $this->url->https ( 'account/login' ) . "\n\n";
			$message .= $this->language->get ( 'mail_services' ) . "\n\n";
			$message .= $this->language->get ( 'mail_thanks' ) . "\n";
			$message .= $this->config->get ( 'config_store' );
			$mail = new Mail ( $this->config->get ( 'config_mail_protocol' ), $this->config->get ( 'config_smtp_host' ), $this->config->get ( 'config_smtp_username' ), html_entity_decode ( $this->config->get ( 'config_smtp_password' ) ), $this->config->get ( 'config_smtp_port' ), $this->config->get ( 'config_smtp_timeout' ) );
			$mail->setTo ( $this->request->post ['email'] );
			$mail->setFrom ( $this->config->get ( 'config_email' ) );
			$mail->setSender ( $this->config->get ( 'config_store' ) );
			$mail->setSubject ( $subject );
			$mail->setText ( $message );
			$this->redirect ( $this->url->https ( 'account/success' ) );
		}
		
		$this->document->breadcrumbs = array ();
		
		$this->document->breadcrumbs [] = array (
				'href' => $this->url->http ( 'common/home' ),
				'text' => $this->language->get ( 'text_home' ),
				'separator' => FALSE 
		);
		
		$this->document->breadcrumbs [] = array (
				'href' => $this->url->http ( 'account/account' ),
				'text' => $this->language->get ( 'text_account' ),
				'separator' => $this->language->get ( 'text_separator' ) 
		);
		
		$this->document->breadcrumbs [] = array (
				'href' => $this->url->http ( 'account/create' ),
				'text' => $this->language->get ( 'text_create' ),
				'separator' => $this->language->get ( 'text_separator' ) 
		);
		
		$this->data ['heading_title'] = $this->language->get ( 'heading_title' );
		
		$this->data ['text_yes'] = $this->language->get ( 'text_yes' );
		$this->data ['text_no'] = $this->language->get ( 'text_no' );
		$this->data ['text_account_already'] = sprintf ( $this->language->get ( 'text_account_already' ), $this->url->https ( 'account/login' ) );
		$this->data ['text_your_details'] = $this->language->get ( 'text_your_details' );
		$this->data ['text_your_address'] = $this->language->get ( 'text_your_address' );
		$this->data ['text_your_password'] = $this->language->get ( 'text_your_password' );
		$this->data ['text_newsletter'] = $this->language->get ( 'text_newsletter' );
		
		$this->data ['entry_firstname'] = $this->language->get ( 'entry_firstname' );
		$this->data ['entry_lastname'] = $this->language->get ( 'entry_lastname' );
		$this->data ['entry_email'] = $this->language->get ( 'entry_email' );
		$this->data ['entry_telephone'] = $this->language->get ( 'entry_telephone' );
		$this->data ['entry_fax'] = $this->language->get ( 'entry_fax' );
		$this->data ['entry_company'] = $this->language->get ( 'entry_company' );
		$this->data ['entry_address_1'] = $this->language->get ( 'entry_address_1' );
		$this->data ['entry_address_2'] = $this->language->get ( 'entry_address_2' );
		$this->data ['entry_postcode'] = $this->language->get ( 'entry_postcode' );
		$this->data ['entry_city'] = $this->language->get ( 'entry_city' );
		$this->data ['entry_country'] = $this->language->get ( 'entry_country' );
		$this->data ['entry_zone'] = $this->language->get ( 'entry_zone' );
		$this->data ['entry_newsletter'] = $this->language->get ( 'entry_newsletter' );
		$this->data ['entry_password'] = $this->language->get ( 'entry_password' );
		$this->data ['entry_confirm'] = $this->language->get ( 'entry_confirm' );
		
		$this->data ['entry_ym'] = $this->language->get ( 'entry_ym' );
		$this->data ['entry_skype'] = $this->language->get ( 'entry_skype' );
		$this->data ['entry_username'] = $this->language->get ( 'entry_username' );
		$this->data ['text_moreinfo'] = $this->language->get ( 'text_moreinfo' );
	
		$this->data ['txtTileCaptcha'] = $this->language->get ( 'txtTileCaptcha' );
		$this->data ['textCaptcha'] = $this->language->get ( 'textCaptcha' );
		$this->data ['errorCaptcha'] = $this->language->get ( 'errorCaptcha' );
		
		
		$this->data ['button_continue'] = $this->language->get ( 'button_continue' );
		
		if (isset ( $this->error ['warning'] )) {
			$this->data ['error_warning'] = $this->error ['warning'];
		} else {
			$this->data ['error_warning'] = '';
		}
		
		if (isset ( $this->error ['firstname'] )) {
			$this->data ['error_firstname'] = $this->error ['firstname'];
		} else {
			$this->data ['error_firstname'] = '';
		}
		
		if (isset ( $this->error ['lastname'] )) {
			$this->data ['error_lastname'] = $this->error ['lastname'];
		} else {
			$this->data ['error_lastname'] = '';
		}
		
		if (isset ( $this->error ['email'] )) {
			$this->data ['error_email'] = $this->error ['email'];
		} else {
			$this->data ['error_email'] = '';
		}
		
		if (isset ( $this->error ['telephone'] )) {
			$this->data ['error_telephone'] = $this->error ['telephone'];
		} else {
			$this->data ['error_telephone'] = '';
		}
		
		if (isset ( $this->error ['password'] )) {
			$this->data ['error_password'] = $this->error ['password'];
		} else {
			$this->data ['error_password'] = '';
		}
		
		if (isset ( $this->error ['confirm'] )) {
			$this->data ['error_confirm'] = $this->error ['confirm'];
		} else {
			$this->data ['error_confirm'] = '';
		}
		
		if (isset ( $this->error ['address_1'] )) {
			$this->data ['error_address_1'] = $this->error ['address_1'];
		} else {
			$this->data ['error_address_1'] = '';
		}
		
		if (isset ( $this->error ['city'] )) {
			$this->data ['error_city'] = $this->error ['city'];
		} else {
			$this->data ['error_city'] = '';
		}
		
		$this->data ['action'] = $this->url->https ( 'account/create' );
		
		if (isset ( $this->request->post ['firstname'] )) {
			$this->data ['firstname'] = $this->request->post ['firstname'];
		} else {
			$this->data ['firstname'] = '';
		}
		
		if (isset ( $this->request->post ['lastname'] )) {
			$this->data ['lastname'] = $this->request->post ['lastname'];
		} else {
			$this->data ['lastname'] = '';
		}
		
		if (isset ( $this->request->post ['email'] )) {
			$this->data ['email'] = $this->request->post ['email'];
		} else {
			$this->data ['email'] = '';
		}
		
		if (isset ( $this->request->post ['telephone'] )) {
			$this->data ['telephone'] = $this->request->post ['telephone'];
		} else {
			$this->data ['telephone'] = '';
		}
		
		if (isset ( $this->request->post ['fax'] )) {
			$this->data ['fax'] = $this->request->post ['fax'];
		} else {
			$this->data ['fax'] = '';
		}
		
		if (isset ( $this->request->post ['company'] )) {
			$this->data ['company'] = $this->request->post ['company'];
		} else {
			$this->data ['company'] = '';
		}
		
		if (isset ( $this->request->post ['address_1'] )) {
			$this->data ['address_1'] = $this->request->post ['address_1'];
		} else {
			$this->data ['address_1'] = '';
		}
		
		if (isset ( $this->request->post ['address_2'] )) {
			$this->data ['address_2'] = $this->request->post ['address_2'];
		} else {
			$this->data ['address_2'] = '';
		}
		
		if (isset ( $this->request->post ['postcode'] )) {
			$this->data ['postcode'] = $this->request->post ['postcode'];
		} else {
			$this->data ['postcode'] = '';
		}
		
		if (isset ( $this->request->post ['city'] )) {
			$this->data ['city'] = $this->request->post ['city'];
		} else {
			$this->data ['city'] = '';
		}
		
		if (isset ( $this->request->post ['country_id'] )) {
			$this->data ['country_id'] = $this->request->post ['country_id'];
		} else {
			$this->data ['country_id'] = $this->config->get ( 'config_country_id' );
		}
		
		if (isset ( $this->request->post ['zone_id'] )) {
			$this->data ['zone_id'] = $this->request->post ['zone_id'];
		} else {
			$this->data ['zone_id'] = 0;
		}
		
		$this->load->model ( 'localisation/country' );
		
		$this->data ['countries'] = $this->model_localisation_country->getCountries ();
		
		if (isset ( $this->request->post ['password'] )) {
			$this->data ['password'] = $this->request->post ['password'];
		} else {
			$this->data ['password'] = '';
		}
		
		if (isset ( $this->request->post ['confirm'] )) {
			$this->data ['confirm'] = $this->request->post ['confirm'];
		} else {
			$this->data ['confirm'] = '';
		}
		
		if (isset ( $this->request->post ['newsletter'] )) {
			$this->data ['newsletter'] = $this->request->post ['newsletter'];
		} else {
			$this->data ['newsletter'] = '';
		}
		
		if ($this->config->get ( 'config_account' )) {
			$this->load->model ( 'catalog/information' );
			
			$information_info = $this->model_catalog_information->getInformation ( $this->config->get ( 'config_account' ) );
			
			if ($information_info) {
				$this->data ['text_agree'] = sprintf ( $this->language->get ( 'text_agree' ), $this->url->http ( 'information/information&information_id=' . $this->config->get ( 'config_account' ) ), $information_info ['title'] );
			} else {
				$this->data ['text_agree'] = '';
			}
		} else {
			$this->data ['text_agree'] = '';
		}
		
		if (isset ( $this->request->post ['agree'] )) {
			$this->data ['agree'] = $this->request->post ['agree'];
		} else {
			$this->data ['agree'] = FALSE;
		}
		
		
		// Yahoo messenger
	
		if (isset ( $this->request->post ['ym'] )) {
			$this->data ['ym'] = $this->request->post ['ym'];
		} else {
			$this->data ['ym'] = FALSE;
		}
		if (isset ( $this->error ['ym'] )) {
			$this->data ['error_ym'] = $this->error ['ym'];
		} else {
			$this->data ['error_ym'] = '';
		}
		
		// Skype
		
		if (isset ( $this->request->post ['skype'] )) {
			$this->data ['skype'] = $this->request->post ['skype'];
		} else {
			$this->data ['skype'] = FALSE;
		}
		
		if (isset ( $this->error ['skype'] )) {
			$this->data ['error_skype'] = $this->error ['skype'];
		} else {
			$this->data ['error_skype'] = '';
		}
	
		// Username
		
		if (isset ( $this->request->post ['username'] )) {
			$this->data ['username'] = $this->request->post ['username'];
		} else {
			$this->data ['username'] = FALSE;
		}
		if (isset ( $this->error ['username'] )) {
			$this->data ['error_username'] = $this->error ['username'];
		} else {
			$this->data ['error_username'] = '';
		}
		
		
		// Captcha 
		
		if (! isset ( $this->data ['error_captchaFlag'] )) {
			$this->data ['error_captchaFlag'] = FALSE;
		}
		
		// Chk_more
		
		if (isset ( $this->request->post ['chk_more'] )) {
			$this->data ['chk_more'] = $this->request->post ['chk_more'];
		} else {
			$this->data ['chk_more'] = FALSE;
		}
		
		$this->id = 'content';
		$this->template = $this->config->get ( 'config_template' ) . 'account/create.html';
		$this->layout = 'common/layout';
		
		$this->render ();
	}
	/**
	 * Check if email is exist
	 * @return true if email is exist, false in other case
	 */
	private function validateMinh() {
		if (($this->request->server ['REQUEST_METHOD'] == 'POST') && $this->model_account_customer->getTotalCustomersByEmail ( $this->request->post ['email'] )) {
			
			return TRUE;
		} else {
			return FALSE;
		}
	}
	/**
	 * Validate user registration form
	 * @return true if information is valid
	 */
	private function validate() {
		//Check for maximum length of first name and last name
		if ((strlen ( utf8_decode ( $this->request->post ['firstname'] ) ) > 32)) {
			$this->error ['firstname'] = $this->language->get ( 'error_firstname' );
		}
		if ((strlen ( utf8_decode ( $this->request->post ['lastname'] ) ) > 32)) {
			$this->error ['lastname'] = $this->language->get ( 'error_lastname' );
		}
		
		//Check if username is null or longer than 30 chars
		if ((strlen ( utf8_decode ( $this->request->post ['username'] ) ) <= 0) || (strlen ( utf8_decode ( $this->request->post ['username'] ) ) > 30)) {
			$this->error ['username'] = $this->language->get ( 'error_username' );
		} 
		//Check if username is existed
		else if ($this->model_account_customer->getTotalCustomersByUserName ( $this->request->post ['username'] )) {
			$this->error ['username'] = $this->language->get ( 'error_username_exists' );
		}
		
		//Check for correct email format
		$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
		if (! preg_match ( $pattern, $this->request->post ['email'] )) {
			$this->error ['email'] = $this->language->get ( 'error_email' );
		}
		
		//Validate length of password
		if ((strlen ( utf8_decode ( $this->request->post ['password'] ) ) < 4) || (strlen ( utf8_decode ( $this->request->post ['password'] ) ) > 20)) {
			$this->error ['password'] = $this->language->get ( 'error_password' );
		}
		//Validate confirm password
		if ($this->request->post ['confirm'] != $this->request->post ['password']) {
			$this->error ['confirm'] = $this->language->get ( 'error_confirm' );
		}
		//Validate length of address and telephone
		if ((strlen ( utf8_decode ( $this->request->post ['address_1'] ) ) > 128)) {
			$this->error ['address_1'] = $this->language->get ( 'error_address_1' );
		}
		if ((strlen ( utf8_decode ( $this->request->post ['telephone'] ) ) > 32)) {
			$this->error ['telephone'] = $this->language->get ( 'error_telephone' );
		}
		
		//Validate captcha
		if (! $this->validCaptcha ()) {
			if (isset ( $this->data ['errorCaptcha'] )) {
				$this->error ['error_Captcha'] = $this->data ['errorCaptcha'];
			}
			$this->data ['error_captchaFlag'] = TRUE;
		} else {
			unset ( $this->error ['error_Captcha'] );
			$this->data ['error_captchaFlag'] = FALSE;
		}
		
		//Check for agree with term 
		if ($this->config->get ( 'config_account' )) {
			$this->load->model ( 'catalog/information' );
			$information_info = $this->model_catalog_information->getInformation ( $this->config->get ( 'config_account' ) );
			if ($information_info) {
				if (! isset ( $this->request->post ['agree'] )) {
					$this->error ['warning'] = sprintf ( $this->language->get ( 'error_agree' ), $information_info ['title'] );
				}
			}
		}
		
		if (! $this->error) {
			unset ( $_SESSION ['captcha_val'], $this->data ['error_captchaFlag'], $_SESSION ['wrong_captcha'] );
			return TRUE;
		} else {
			return FALSE;
		}
	}
	/**
	 * Check for captcha
	 * @return true if captcha is correct, false in other case
	 */
	private function validCaptcha() {
		$kq = TRUE;
		if ($this->request->post ['valid_captcha'] != $_SESSION ['captcha_val']) {
			$kq = FALSE;
		}
		return $kq;
	}
	
	/**
	 * Get all zone and return html list code
	 */
	public function zone() {
		$output = '<select style="width: 150px;" name="zone_id">';
		
		$this->load->model ( 'localisation/zone' );
		
		$results = $this->model_localisation_zone->getZonesByCountryId ( $this->request->get ['country_id'] );
		
		foreach ( $results as $result ) {
			$output .= '<option value="' . $result ['zone_id'] . '"';
			
			if (isset ( $this->request->get ['zone_id'] ) && ($this->request->get ['zone_id'] == $result ['zone_id'])) {
				$output .= ' selected="selected"';
			}
			
			$output .= '>' . $result ['name'] . '</option>';
		}
		
		if (! $results) {
			$output .= '<option value="0">' . $this->language->get ( 'text_none' ) . '</option>';
		}
		
		$output .= '</select>';
		
		$this->response->setOutput ( $output );
	}
}
?>